"""
YouTour - Create and access tours based on public transport routes. 
Copyright (C) 2011 Karl Beecher

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from google.appengine.ext import webapp
from settings import PROJECT_PATH
from google.appengine.ext.webapp import template
import os, logging
from business.TourFactory import TourFactory
from datetime import datetime
from business.StopFactory import StopFactory
from google.appengine.api.blobstore import blobstore

class UploadTourPage(webapp.RequestHandler):

    def get(self):

        templateValues = {}
        path = os.path.join(PROJECT_PATH, "templates/uploadTour.html")

        self.response.out.write(template.render(path, templateValues))


    def post(self):
        title = self.request.get("title")
        description = self.request.get("description")
        startPosition = self.request.get("startPosition")
        endPosition = self.request.get("endPosition")
        startTime = self.request.get("startTime")
        endTime = self.request.get("endTime")

        newTour = TourFactory.buildTour()
        newTour.title = title
        newTour.description = description
        newTour.startPosition = startPosition
        newTour.endPosition = endPosition
        newTour.startTime = datetime.time(datetime.now())
        newTour.endTime = datetime.time(datetime.now())

        stopNumber = self.request.get("stopNumber")
        stopDescription = self.request.get("stopDescription")
        directions = self.request.get("stopDirections")

        newStop = StopFactory.buildStop()
        newStop.stopNumber = int(stopNumber)
        newStop.description = stopDescription
        newStop.directions = directions

        newTour.stopList.append(newStop)
        newTour.dbStore()

        self.redirect("/")
